package com.jy.oms.framework.thread;

import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 自定义线程池
 *
 * @author liuquan
 */
public class CustomThreadPoolExecutor extends ThreadPoolExecutor {
    private static AtomicInteger threadPoolId = new AtomicInteger(0);

    private int poolId;
    private String threadPoolName;

    public CustomThreadPoolExecutor(String threadPoolName,
                                    int corePoolSize,
                                    int maximumPoolSize,
                                    long keepAliveTime,
                                    TimeUnit unit,
                                    BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
        this.poolId = threadPoolId.incrementAndGet();
        this.threadPoolName = threadPoolName;
        monitor();
    }

    public CustomThreadPoolExecutor(String threadPoolName,
                                    int corePoolSize,
                                    int maximumPoolSize,
                                    long keepAliveTime,
                                    TimeUnit unit,
                                    BlockingQueue<Runnable> workQueue,
                                    ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
        this.poolId = threadPoolId.incrementAndGet();
        this.threadPoolName = threadPoolName;
        monitor();
    }

    public CustomThreadPoolExecutor(String threadPoolName,
                                    int corePoolSize,
                                    int maximumPoolSize,
                                    long keepAliveTime,
                                    TimeUnit unit,
                                    BlockingQueue<Runnable> workQueue,
                                    RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
        this.poolId = threadPoolId.incrementAndGet();
        this.threadPoolName = threadPoolName;
        monitor();
    }

    public CustomThreadPoolExecutor(String threadPoolName,
                                    int corePoolSize,
                                    int maximumPoolSize,
                                    long keepAliveTime,
                                    TimeUnit unit,
                                    BlockingQueue<Runnable> workQueue,
                                    ThreadFactory threadFactory,
                                    RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
        this.poolId = threadPoolId.incrementAndGet();
        this.threadPoolName = threadPoolName;
        monitor();
    }

    public String getThreadPoolName() {
        return threadPoolName;
    }

    public int getPoolId() {
        return poolId;
    }

    private void monitor() {
        CustomThreadPoolManager.getInstance().register(this);
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t, r);
    }

    @Override
    public void shutdown() {
        CustomThreadPoolManager.getInstance().unregister(this);
        super.shutdown();
    }

    @Override
    public List<Runnable> shutdownNow() {
        CustomThreadPoolManager.getInstance().unregister(this);
        return super.shutdownNow();
    }
}
